/**
 * 
 */
package org.swing.utility.jai.util;
import java.awt.Dimension;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.media.jai.JAI;

import org.swing.utility.jai.bean.ImageSize;
import org.swing.utility.jai.imp.ImageAction;
/**
 * @author Quynh Nhu
 *
 */
public class JaiImageUtil implements ImageAction {
	  public JaiImageUtil() {
	    JAI.setDefaultTileSize(new Dimension(50, 50));

	  }

	  public void scale(final File srcFile, final File destFile, final int width,
	    final int height) {
	    try {
	      RenderedImage image = loadImage(srcFile);
	      scale(srcFile, image, destFile, width, height);
	    } catch (IOException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    }
	  }

	  private void scale(final File srcFile, final RenderedImage image,
	    final File destFile, final int width, final int height) throws IOException {
	    destFile.getParentFile().mkdirs();
	    RenderedImage resizedImage = JaiUtils.scaleToSize(image, width, height);
	    ImageWriter jpegWriter = (ImageWriter)ImageIO.getImageWritersByMIMEType(
	      "image/jpeg").next();
	    jpegWriter.setOutput(ImageIO.createImageOutputStream(destFile));
	    jpegWriter.write(resizedImage);
	    jpegWriter.dispose();
	    destFile.setLastModified(srcFile.lastModified());
	  }

	  public void scale(final File srcFile, final File destFile,
	    final ImageSize size) {
	    scale(srcFile, destFile, size.getWidth(), size.getHeight());
	  }

	  public void scale(final File srcFile, final List destFiles, final List sizes) {
	    try {
	      RenderedImage srcImage = loadImage(srcFile);
	      Iterator fileIter = destFiles.iterator();
	      Iterator sizeIter = sizes.iterator();
	      while (sizeIter.hasNext() && fileIter.hasNext()) {
	        ImageSize size = (ImageSize)sizeIter.next();
	        File destFile = (File)fileIter.next();
	        scale(srcFile, srcImage, destFile, size.getWidth(), size.getHeight());
	      }
	    } catch (IOException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    }
	  }

	  private RenderedImage loadImage(final File file) throws IOException {
	    RenderedImage image = JaiUtils.loadImage(file);
	    if (image == null) {
	      throw new FileNotFoundException();
	    }
	    return image;
	  }

	  public void thumbnail(File srcFile, File destFile, int width, int height) {
	    // TODO Auto-generated method stub
	    
	  }

	  public void thumbnail(File file, File fileName, ImageSize thumbnailSize) {
	    // TODO Auto-generated method stub
	    
	  }
	  
	  public static void main(String[] args) {
	        String inputImage = "/home/lqnhu/TEST/31856_a015376-00149.j2k";
	        String oututImage = "/home/lqnhu/TEST/Pic10.jpg";
	        
	        JaiImageUtil im = new JaiImageUtil();
	        im.scale(new File(inputImage), new File(oututImage), 500,500);
	}


	}
